package com.coreservlets.jsf.ss07;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

public class SearchUtilities
{
	private static SearchSpec[] commonSpecs = { new SearchSpec( "Google", "http://www.google.com/search?q="),
												new SearchSpec( "Yahoo", "http://search.yahoo.com/bin/search?p=" ),
												new SearchSpec( "Bing", "http://www.bing.com/search?q="),
												new SearchSpec( "AllTheWeb", "http://www.alltheweb.com/search?q="),
												new SearchSpec( "AltaVista", "http://www.altavista.com/web/results?q=" ),
												new SearchSpec( "Lycos", "http://search.lycos.com/default.asp?query="),
												new SearchSpec( "HotBot", "http://hotbot.com/default.asp?query=" ), };
	
	private static List<SelectItem> searchEngineNames;
	
	static
	{
		searchEngineNames = new ArrayList< SelectItem>();
		
		for( SearchSpec spec: commonSpecs )
		{
			searchEngineNames.add( new SelectItem( spec.getSearchEngineName()));
		}
	}
	
	public static SearchSpec[] commonSearchSpecs()
	{
		return( commonSpecs );
	}
	
	public static List<SelectItem> searchEngineNames()
	{
		return( searchEngineNames );
	}
	
	/**
	 * Given a search engine name and a search string, builds
	 * a URL for the results page of that search engine
	 * for that query. Returns null if the search engine name
	 * is not one of the ones it knows about.
	 */
	
	public static String makeURL( String searchEngineName, String searchString )
	{
		String searchURL = null;
		for( SearchSpec spec: commonSpecs )
		{
			if( spec.getSearchEngineName().equalsIgnoreCase( searchEngineName ) )
			{
				searchURL = spec.makeURL(searchString);
			}
		}
		
		return( searchURL );
	}
	
	private SearchUtilities() {} // Uninstantiatable class
}

